package com.cubic.qr.scanner;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import com.google.zxing.integration.android.IntentIntegrator;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	IntentIntegrator integrator = new IntentIntegrator(this);	
	String sResults= "";
	ArrayList<String> searchSerial = new ArrayList<String>();
	String[] snArray;
	ArrayList<String> foundArray = new ArrayList<String>();
	int foundIndex = 0;
	String file =  "/cubic.csv";
	File dir = new File(Environment.getExternalStorageDirectory() + "//CubicQRScanner//");
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if (!dir.exists())
        {
        	dir.mkdirs();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void HandleButtonClick(View v)
    {   	
    	integrator.initiateScan();    	
    }
    
    
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    	Boolean found = false;
    	TextView results=(TextView)findViewById(R.id.scanResults);
    	    	
    		if (resultCode == RESULT_OK)
   		   	{   			   
   			   sResults = intent.getStringExtra("SCAN_RESULT");
   			   snArray = sResults.split("\\,"); 
   			   for (int i = 0; i < snArray.length; i++)
   			   {
   				   for (int j = 0; j < searchSerial.size(); j++)
   				   {
	   				   if (snArray[i].equals(searchSerial.get(j)))
	   				   {	   					   
	   					   results.setText("Match Found");
	   					   found = true;
	   					   foundArray.add(snArray[i]);	
	   					   foundIndex++;
	   					   
	   				   }
   				   
   				   }
   			   }
   			   
   			   if (found == true)
   			   {
   				displayDeviceFound();
   			   }
   			   else 
   			   {
   				  integrator.initiateScan();
   				  
   			   }
   			
   		   }
   		   else if (resultCode == RESULT_CANCELED)
   		   {
   			   return;
   			   
   			   
   		   }
    	    	  
    	  // else continue with any other code you need in the method
    	  
    	}
    
    public void storeSearchSerial(View v)
    {
    	TextView tvStatus=(TextView)findViewById(R.id.Test);
    	searchSerial.add(tvStatus.getText().toString().trim());
    	tvStatus.setText("");
    }
    
   public void displayDeviceFound()
   {
	   new AlertDialog.Builder(this)
	   .setTitle("Devices Found")
	   .setMessage("These Devices Were Found in this box: " + foundArray)
	   .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		
		public void onClick(DialogInterface dialog, int which) {
			foundArray.clear();
			integrator.initiateScan();
			
		}
		
	}).show();
   }//End displayDeviceFound()
   
      
   
   
   public void fileProcessing(View v)
   {
	   String s = "null";
	   try {
		BufferedReader br = new BufferedReader(new FileReader(dir + file));
		s = br.readLine();
		searchSerial.addAll(Arrays.asList(s.split("\\,")));
		br.close();				
	} catch (FileNotFoundException e) {
		TextView results=(TextView)findViewById(R.id.scanResults);
		results.setText("File Not found!" + dir + file);
		e.printStackTrace();
	} catch (IOException e) {
		TextView results=(TextView)findViewById(R.id.scanResults);
		results.setText("Something Something about IO");
		
		e.printStackTrace();
	}
	   
	     
   }
    
}